#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN MVC MODULE FOR THN WEB SERVER

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For use in commercial software, please obtain a commercial license from THN Solutions LLC.    

  + For use in free software, this library is licensed under GPL version 3. A summary of 
	GPLv3 is listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt 
	included with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 18-Apr-13 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;

using Thn;
#endregion

namespace Thn.Web.Mvc
{
    /// <summary>
    /// This module is responsible for configuring MVC web server.
    /// </summary>
#if DEBUG || TESTING
    public
#else
    public
#endif
 class MvcModule : IWebServerModule
    {
        #region Fields
        #endregion

        #region Properties

        #region Root View Path
        private string mRootViewPath = "Views";
        /// <summary>
        /// Gets or sets physical path to root of all views.
        /// </summary>
        public string RootViewPath
        {
            get { return mRootViewPath; }
            set { mRootViewPath = value; }
        }
        #endregion

        #region View Path Translator
        private IPathTranslator mViewPathTranslator;
        /// <summary>
        /// Gets or sets the translator responsible for resolving path to views.
        /// </summary>
        public IPathTranslator ViewPathTranslator
        {
            get { return mViewPathTranslator; }
            set { mViewPathTranslator = value; }
        }
        #endregion

        #region View Renderer
        private IViewRenderer mViewRenderer;
        /// <summary>
        /// Gets or sets the renderer responsible for rendering views.
        /// </summary>
        public IViewRenderer ViewRenderer
        {
            get { return mViewRenderer; }
            set { mViewRenderer = value; }
        }
        #endregion

        #region Controller Manager
        private IControllerManager mControllerManager;
        /// <summary>
        /// Gets or sets the view controller manager.
        /// </summary>
        public IControllerManager ControllerManager
        {
            get { return mControllerManager; }
            set { mControllerManager = value; }
        }
        #endregion
        
        #endregion

        #region IWebServerModule Members

        #region Initialize
        /// <summary>
        /// Configures and starts this module for the provided target.
        /// </summary>
        /// <param name="target">The web server which this modules will run in.</param>
        void IWebServerModule.Initialize(WebServer target)
        {
            //get renderer if not specifically assigned
            if (mViewRenderer == null) mViewRenderer = Services.Get<IViewRenderer>();

            //create path translator if not specifically assigned
            if (mViewPathTranslator == null)
            {
                UriToLocalPathTranslator translator = new UriToLocalPathTranslator();
                translator.Root = mRootViewPath;
                mViewPathTranslator = translator;
                mViewRenderer.PathTranslator = translator;
            }

            //create controller manager if not assigned
            if (mControllerManager == null)
            {
                ControllerManager man = new ControllerManager();
                man.Renderer = mViewRenderer;
                mControllerManager = man;
            }

            //register handler as the very first handler
            MvcRequestHandler handler = new MvcRequestHandler();
            handler.ControllerManager = mControllerManager;
            target.RequestHandlers.Insert(0, handler);
            //target.RequestHandlers.Add(handler);            
        }
        #endregion

        #endregion

        #region Constructors
        /// <summary>
        /// Create a new instance.
        /// </summary>
        public MvcModule()
            : base()
        { }
        #endregion
    }
}
